//
//  FirstSectionViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/8.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "FirstSectionViewController.h"

@interface FirstSectionViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, XrLayoutItemHeightDelegate>

@property (nonatomic, retain) BaseCollectionView *collectionView;  /**< collection */
@property (nonatomic, retain) NSDictionary *dicTop;
@property (nonatomic, retain) NSMutableArray *arrData;
@property (nonatomic, assign) NSInteger page;/**< 用来保存请求新数据动态值 */
@property (nonatomic, retain) NSString *collecTitle;
@property (nonatomic, retain) FirstSectionHeaderView *viewTop;
@end

@implementation FirstSectionViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _collectionView.delegate = nil;
    _collectionView.dataSource = nil;
    [_collectionView release];
    [_dicTop release];
    [_arrData release];
    [_collecTitle release];
    [_viewTop release];
    [_strID release];
    [_strTitle release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = _strTitle;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self dataTop];
 
    [self creatCollectionView];
 
}
#pragma mark - ------------ 头部视图数据处理 ---------------
- (void)dataTop
{

    NSString *str = @"http://203.80.144.213/napi/theme/detail/?platform_version=4.4.4&device_platform=L36h&__dtac=%257B%2522_r%2522%253A%2520%2522666681%2522%257D&screen_width=1080&screen_height=1776&__domain=www.duitang.com&app_version=67&theme_id=554c8abaa310def67913c5c0&platform_name=Android&locale=zh&app_code=nayutas";
    NSString *url = [str stringByReplacingOccurrencesOfString:@"theme_id=554c8abaa310def67913c5c0" withString:_strID];
    
    [XrDataParse GETWithURL:url par:nil success:^(id responseObject) {
       
        self.dicTop = responseObject[@"data"];
        self.collecTitle = _dicTop[@"data_src"][@"filter_id"];
        
        [self dataHandler];
        
        [_collectionView reloadData];
        [self createTopView];
        
    } filed:^(NSError *error) {
        
    }];

   
}

#pragma mark - ------------ 数据处理  ---------------

- (void)dataHandler
{
    
  NSString *str = @"http://203.80.144.213/napi/blog/list/by_filter_id/?include_fields=sender%2Calbum%2Cicon_url%2Clike_count%2Creply_count&platform_version=4.4.4&filter_id=%E4%BB%8A%E6%97%A5TOP%E6%A6%9C&device_platform=L36h&__dtac=%257B%2522_r%2522%253A%2520%2522666681%2522%257D&screen_width=1080&screen_height=1776&__domain=www.duitang.com&app_version=67&start=0&platform_name=Android&locale=zh&app_code=nayutas";
    
    //拼接题目 中文
        NSString *url = [str stringByReplacingOccurrencesOfString:@"filter_id=%E4%BB%8A%E6%97%A5TOP%E6%A6%9C" withString:[NSString stringWithFormat:@"filter_id=%@", _collecTitle]];
    //需要中文转码
    NSString *urlStr = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:url]];
   
        [XrDataParse GETWithURL:urlStr par:nil success:^(id responseObject) {
            NSArray *arr = responseObject[@"data"][@"object_list"];
            
            self.arrData = [FindDeatilModel modelHandleWithArrar:arr];
            //线程 block有调用才走
            [_collectionView reloadData];
            
        } filed:^(NSError *error) {
            
        }];
    
}

#pragma mark - ------------ 创建头部文本自适应 ---------------
- (void)createTopView
{
    
    CGFloat h = [FirstSectionHeaderView heightWithText:_dicTop[@"desc"]];
    _collectionView.contentInset = UIEdgeInsetsMake(205 + h, 0, 0, 0);
    self.viewTop = [[FirstSectionHeaderView alloc]initWithFrame:CGRectMake(0, -205-h, SCREEN_WIDTH, 205+ h)];
    _viewTop.dicTop = _dicTop;
    [_collectionView addSubview:_viewTop];
    [_viewTop release];
}


#pragma mark - ------------ 瀑布流layout---------------
- (void)creatCollectionView
{
    
    // 自定义layout
    XrLayout *layout = [[XrLayout alloc] init];
    //列数
    layout.columCounts = 2;
    //列间距
    layout.columSpace = 10;
    //行间距
    layout.rowSpace = 20;
    //边距
    layout.edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    //代理
    layout.delegate = self;
    
    
    //下方collectionView
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 49) collectionViewLayout:layout];
    //collection代理
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = YES;
    /** 夜间 */
    _collectionView.dk_backgroundColorPicker = DKColorWithColors(GrayLight,[UIColor grayColor]);
    
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[FindDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [layout release];
    [_collectionView release];
    
}


#pragma mark - ------------ 赋值 ---------------
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
   if (_arrData.count != 0) {
       
        cell.backgroundColor = [UIColor whiteColor];
        //cell加个阴影
        cell.layer.shadowOffset = CGSizeMake(0.1, 0.1);
        cell.layer.shadowOpacity = 0.3;
        cell.layer.shadowRadius = 1;
       
       FindDeatilModel *m = _arrData[indexPath.item];
       cell.labelContent.text = m.msg;
       cell.model = m ;
   }
    return cell;
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _arrData.count;
    
    
}


#pragma mark - ------------ 点击跳转详情 ---------------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DetailDetailViewController *detail = [[DetailDetailViewController alloc]init];
    FindDeatilModel *m = _arrData[indexPath.item];
    
    detail.detailID = m.iid.stringValue;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];
    
}




#pragma mark - ------------ 设置高度 ---------------
- (CGFloat)layout:(XrLayout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width
{
    
    FindDeatilModel *m = _arrData[indexPath.item];
    //图片高度
    CGFloat imgH = width * m.height.doubleValue / m.width.doubleValue;
    //文字高度
    CGFloat h1 = [FindDetailCollectionViewCell heightWithText:m.msg];
    //返回行数
    if (h1 >= 15.51 * 6) {
        h1 = 15.51 * 6;
    }
    
    // 每个cell的高度 = 图片高度 和 文本高度  + 其他控件高度(80)
    return imgH + h1 + 106 ;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
